package theOrbalEra.client.gui;


import java.util.ArrayList;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.entity.RenderItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;

import org.lwjgl.opengl.GL11;

import theOrbalEra.common.block.OrbalBlockQuatzExchange;
import theOrbalEra.common.item.OrbalItemEleBag;
import theOrbalEra.common.item.OrbalItemElement;
import theOrbalEra.common.network.PacketGetQuartz;
import theOrbalEra.common.util.DefultValue;
import theOrbalEra.common.util.OrbalUtils;
import theOrbalEra.common.util.GetName;
import theOrbalEra.common.util.QuartzRepices;

import cpw.mods.fml.client.FMLClientHandler;

public class GuiQuatzExchange extends GuiScreen {
	public OrbalBlockQuatzExchange currentBlock;
	private EntityPlayer player;
	RenderItem ironRenderer = new RenderItem();
	private int current=0;
	private int totalLong=-1;
	private boolean[] use={false,false,false,false,false,false,false};
	GuiButton make;
    GuiButtonQuatzExchangeNext pre;
    GuiButtonQuatzExchangeNext pos;
    private boolean enough=true;
    private int tick=0;
    
	public GuiQuatzExchange(Block block, EntityPlayer player) {
		this.currentBlock = (OrbalBlockQuatzExchange) block;
		mc = FMLClientHandler.instance().getClient();
		this.player = player;
		

	}

	@Override
	public boolean doesGuiPauseGame() {
		return false;
	}

	@Override
	public void drawScreen(int par1, int par2,float par3) {
		
		drawDefaultBackground();
		GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
		mc.renderEngine.bindTexture(DefultValue.Gui_QuatzExchange);
		
		int xSize=175;
		int ySize=222;
		int var5 = (this.width - xSize) / 2;
        int var6 = (this.height -ySize) / 2;
        this.drawTexturedModalRect(var5, var6, 0, 0, xSize,ySize);
        
        
        
        	drawSwitch(var5,var6);
        	List<Integer> canExchange=getCanExchange();
        	this.totalLong=canExchange.size();
        	if(this.totalLong>0)
        drawCanExchange(canExchange,var5,var6,xSize,ySize);
        
        super.drawScreen(par1, par2, par3);
        drawCountString(var5,var6,xSize,ySize);   
        drawPlayerInv(var5,var6,xSize,ySize);
        
       
        if(!this.enough)
        	this.fontRenderer.drawString(GetName.localize("display.notEnough"), var5+106, var6+120, 16777215);
       
	}


	private void drawSwitch(int var5, int var6) {
		for(int i=0;i<use.length;i++){
			if(use[i]==false)
				this.drawTexturedModalRect(var5+4,var6+17+18*i,215, 1, 10, 10);
			else{
				this.drawTexturedModalRect(var5+4,var6+17+18*i,215, 13, 10, 10);
				
			}
				
		}
		

		
	}

	private void drawCanExchange(List<Integer> canExchange, int var5, int var6, int xSize, int ySize) {
		int onSell=canExchange.get(current);
		int ele=OrbalUtils.fromLoopIdGetEle(onSell);
		GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
		GL11.glScaled(1.0, 1.0, 1.0);
	
		mc.renderEngine.bindTexture(DefultValue.Gui_QuatzExchange_item);
		this.drawTexturedModalRect(var5+93,var6+68, 0+16*ele, 0, 16, 16);
		String name=GetName.localize("quartz.".concat(GetName.getQuartzName(onSell)));
		this.fontRenderer.drawString(name, var5+122+44/2-fontRenderer.getStringWidth(name)/2,var6+ 72, 0);
	}

	private List<Integer> getCanExchange() {
		
		List<Integer> canExchange=new ArrayList<Integer>();
		int[] have={0,0,0,0,0,0,0};
	    int[] bagHave={0,0,0,0,0,0,0};
		int[] invHave={0,0,0,0,0,0,0};
		if(OrbalItemEleBag.hasbag(player)){
			ItemStack[] main=player.inventory.mainInventory;
			for(ItemStack stack:main){
				if(stack!=null&&stack.getItem() instanceof OrbalItemEleBag){
					bagHave=OrbalItemEleBag.getEleCount(stack);
				}
			}
		}
		invHave=OrbalItemElement.eleInInventory(player);
		for(int i=0;i<7;i++){
		have[i]=bagHave[i]+invHave[i];
		}
		
        canExchange=OrbalUtils.canExchange(canExchange,have,use);
        return canExchange;
		
	}

	@Override
	public void updateScreen() {
		int max=getCanExchange().size();
		if(this.current!=max-1&&max!=0)
			this.pos.enabled=true;
		else
			this.pos.enabled=false;
			
		if(this.current==0)
			this.pre.enabled=false;
		else
			this.pre.enabled=true;
		if(!this.enough){
			this.tick++;
		if(this.tick==20*5){
			this.enough=true;
			this.tick=0;
		}
		}
		
		super.updateScreen();
	}

	private void drawPlayerInv(int var5, int var6, int xSize, int ySize) {
		ItemStack[] playerinv=player.inventory.mainInventory;
		for(int i=0;i<9;i++){
			
		this.ironRenderer.renderItemAndEffectIntoGUI(fontRenderer, mc.renderEngine, playerinv[i], var5+8+18*i, var6+198);
		}

		for (int var1 = 0, i=9; var1 < 3; ++var1)
        {
            for (int var2 = 0; var2 < 9; ++var2)
            {
            	this.ironRenderer.renderItemAndEffectIntoGUI(fontRenderer, mc.renderEngine, playerinv[i],  var5+8 + var2 * 18,var6+140+var1 * 18);
            i++;
            }
        }
		
	}

	private void drawCountString(int outsetX, int outsetY, int xSize, int ySize) {
	 
		int[] have={0,0,0,0,0,0,0};
	    int[] bagHave={0,0,0,0,0,0,0};
		int[] invHave={0,0,0,0,0,0,0};
		if(OrbalItemEleBag.hasbag(player)){
			ItemStack[] main=player.inventory.mainInventory;
			for(ItemStack stack:main){
				if(stack!=null&&stack.getItem() instanceof OrbalItemEleBag){
					int[] bag=OrbalItemEleBag.getEleCount(stack);
					for(int i=0;i<bagHave.length;i++){
					bagHave[i]+=bag[i];
					}
				}
			}
		}
		invHave=OrbalItemElement.eleInInventory(player);
		for(int i=0;i<7;i++){
		have[i]=bagHave[i]+invHave[i];
		}
		int var1=outsetX+27;
		int var2=outsetY+17;
		int var3=19;
		int var4=12;
		for(int i=0;i<7;i++){
		this.fontRenderer.drawString(String.valueOf(have[i]), var1+var3/2-this.fontRenderer.getStringWidth(String.valueOf(have[i]))/2, var2+var4/2-4+18*i, 16777215);
		//AdvUtils.log(String.valueOf(have[i]));
		}
	}



	@Override
	protected void actionPerformed(GuiButton button) {
	if (button == pre){
		if(this.current-1>=0)
			this.current-=1;
		
	}
	if (button == pos){
		if(this.current+1<getCanExchange().size())
			this.current+=1;
		
	}
	if (button == make){
		if(player.inventory.getFirstEmptyStack()!=-1){
			if(getCanExchange().size()>0)
			getQuatz();
			}
		else{
			this.enough=false;
			
		}
		
	}
			
		super.actionPerformed(button);
	}

	
	

	private void getQuatz() {
		int[] have={0,0,0,0,0,0,0};
		if(OrbalItemEleBag.hasbag(player)){
			ItemStack[] main=player.inventory.mainInventory;
			for(ItemStack stack:main){
				if(stack!=null&&stack.getItem() instanceof OrbalItemEleBag){
					int[]bag=OrbalItemEleBag.getEleCount(stack);
					for(int i=0;i<stack.stackSize;i++)
					{
						have[i]+=bag[i];
						}
					}
				}
		}
		int[] eleHave=OrbalItemElement.eleInInventory(player);
		for(int i=0;i<eleHave.length;i++){
			have[i]+=eleHave[i];
			
		}
		
		List<Integer> canExchange=getCanExchange();
		int loopid=canExchange.get(current);
		int[] need =QuartzRepices.quartzRepices.getneed(loopid);
		
		PacketGetQuartz.getQuatz(loopid, have, need);
		
		
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public void initGui() {
		super.initGui();
		int xSize=175;
		int ySize=222;
		int var5 = (this.width - xSize) / 2;
        int var6 = (this.height -ySize) / 2;
		pre  = new  GuiButtonQuatzExchangeNext(0,var5+101-7,var6+60-10,false);
		pos  = new  GuiButtonQuatzExchangeNext(1,var5+101-7,var6+91,true);
	    make = new  GuiButton(2,var5+126,var6+91, 36, 20, GetName.localize("button.make"));  
	    this.buttonList.add(pre);
		this.buttonList.add(pos);
		this.buttonList.add(make);
		this.pre.enabled=false;
		this.pos.enabled=false;
	

	}
	@Override
	protected void mouseClicked(int mXsize, int mYsize, int chicked) {
		super.mouseClicked(mXsize, mYsize, chicked);
		int xSize=175;
		int ySize=222;
		int begX = (this.width - xSize) / 2;
        int begY = (this.height -ySize) / 2;
		if(mXsize>=begX+4&&mXsize<=begX+14&&mYsize>=begY+17&&mYsize<=begY+27){
			use[0]=!use[0];
		}
		if(mXsize>=begX+4&&mXsize<=begX+14&&mYsize>=begY+35&&mYsize<=begY+45){
			use[1]=!use[1];
		}
		if(mXsize>=begX+4&&mXsize<=begX+14&&mYsize>=begY+53&&mYsize<=begY+63){
			use[2]=!use[2];
		}
		if(mXsize>=begX+4&&mXsize<=begX+14&&mYsize>=begY+71&&mYsize<=begY+81){
			use[3]=!use[3];
		}
		if(mXsize>=begX+4&&mXsize<=begX+14&&mYsize>=begY+89&&mYsize<=begY+99){
			use[4]=!use[4];
		}
		if(mXsize>=begX+4&&mXsize<=begX+14&&mYsize>=begY+107&&mYsize<=begY+117){
			use[5]=!use[5];
		}
		if(mXsize>=begX+4&&mXsize<=begX+14&&mYsize>=begY+125&&mYsize<=begY+135){
			use[6]=!use[6];
		}

	}

	




	

}
